#include <my_header.h>
#include <string.h>
#define BUF_SIZE 4096

typedef struct {
    char name[256];
    int size;
    char data[BUF_SIZE];
} file_packet;

/* Usage: mkfifo 创建管道文件 */
int main(int argc, char *argv[]){    
    if(argc <= 1) return -1;
    
    // Create pipe
    int ret = mkfifo("data.pipe", 0644);
    ERROR_CHECK(ret, -1, "make pipe");
    int fd = open("data.pipe", O_WRONLY);
    ERROR_CHECK(fd, -1, "open pipe");
   
//    for(int i = 1; i < argc; i++){
        int i = 1;

        // open file
        int file_fd = open(argv[i], O_RDONLY);
        ERROR_CHECK(file_fd, -1, "open file");

        // create file object
        struct stat st;
        fstat(file_fd, &st);
        file_packet packet;
    
        // get file info
        strncpy(packet.name, argv[i], sizeof(packet.name));
        packet.size = st.st_size;
        ssize_t bytes_read = read(file_fd, packet.data, sizeof(packet.data));
        
        // send data with pipe
        if(bytes_read > 0){
            write(fd, &packet, sizeof(file_packet));
        }

        // relax
        sleep(1);
//    }
}
